/**
 * controll userObj for all
 * 1.class use example ://class组件使用示例
 * @example
 * this.state = {
            userB: {
                name: "tommy"
            }
        };
        var _self = this;
        function updateState(value) {
            _self.setState({
                userB: value.user
            });
        }
        // Uts.regHook(this, function(obj) {
        UserApi.addUserStateHook(updateState, 'userB');

   //function use example://function组件使用示例
   const [state, setState] = React.useState({
        user: {
            name: "userA"
        }
    });
    UserApi.addUserStateHook(setState);
 */

let UserApi = {
    userInfo: {},
    userStateHookList: [],
    addUserStateHook: function(cb, paramName) {
        if (typeof cb === "function") {
            if (this.userStateHookList.indexOf(cb) === -1) {
                this.userStateHookList.push(cb);
            }
        }
    },
    updateUserState: function(value) {
        this.userInfo = value;
        for (let i = this.userStateHookList.length - 1; i >= 0; i--) {
            let item = this.userStateHookList[i];
            item(value);
        }
    },
    removeUserStateHook: function(cb) {
        let ps = this.userStateHookList.indexOf(cb);
        if (ps > -1) {
            this.userStateHookList.splice(ps, 1);
        }
    },
    userLogin: function() {},
    toLogin: function() {},
    userLogout: function() {},
    getUserInfo: function() {
        return this.userInfo;
    },
    setUserInfo: function(value) {
        this.updateUserState(value);
    }
};

global.UserApi = UserApi;

export default UserApi;
